package com.white.commonlib.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.white.commonlib.R;
import com.zhy.autolayout.utils.AutoUtils;

/**
 * Created by yutf on 2017/6/20 0020.
 */

public class SettingItem extends RelativeLayout
{
    private int gloablePadding = 30;
    private int drawablePadding = 20;

    private @DrawableRes int itemLeftIcon;
    private @DrawableRes int itemRightIcon;
    private @DrawableRes int background;

    private String text;
    private int textSize = 30;
    private int textColor = 0xFF989898;

    private Context mContext;

    public SettingItem(Context context)
    {
        this(context, null);
    }

    public SettingItem(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        mContext = getContext();
        initAttrs(attrs);
        initContent();
    }

    private static final int[] ATTRS = new int[]{
            android.R.attr.textSize, android.R.attr.textColor,
            android.R.attr.padding, android.R.attr.text};

    private void initAttrs(AttributeSet attrs)
    {
        TypedArray t = mContext.obtainStyledAttributes(attrs, R.styleable.SettingItem);

        DisplayMetrics dm = getResources().getDisplayMetrics();
        gloablePadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, gloablePadding, dm);
        drawablePadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, drawablePadding, dm);
        textSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, textSize, dm);

        drawablePadding = t.getDimensionPixelSize(R.styleable.SettingItem_drawablePadding, drawablePadding);

        itemLeftIcon = t.getResourceId(R.styleable.SettingItem_itemLeftIcon, 0);
        itemRightIcon = t.getResourceId(R.styleable.SettingItem_itemRightIcon, 0);
        background = t.getResourceId(R.styleable.SettingItem_background, 0);

        t.recycle();

        TypedArray a = mContext.obtainStyledAttributes(attrs, ATTRS);
        textSize = a.getDimensionPixelSize(0, textSize);
        textColor = a.getColor(1, textColor);
        gloablePadding = a.getDimensionPixelSize(2, gloablePadding);
        text = (String) a.getText(3);
        if(TextUtils.isEmpty(text))
        {
            text = "";
        }
        a.recycle();
    }


    private void initContent()
    {
        gloablePadding = AutoUtils.getPercentHeightSizeBigger(gloablePadding);
        setPadding(gloablePadding, gloablePadding, gloablePadding, gloablePadding);
        createTextView();

        setClickable(true);
        if(background != 0)
        {
            setBackgroundResource(background);
        }

        createMoreImage();
    }

    private void createTextView()
    {
        TextView tv = new TextView(mContext);
        tv.setText(text);
        tv.setTextColor(textColor);
        tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
        tv.setGravity(Gravity.CENTER_VERTICAL);
        LayoutParams p = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        p.addRule(RelativeLayout.CENTER_VERTICAL);
        if(itemLeftIcon != 0)
        {
            Drawable d = getResources().getDrawable(itemLeftIcon);
            if(d != null)
            {
                d.setBounds(0,0,d.getMinimumWidth(), d.getMinimumHeight());
                tv.setCompoundDrawables(d,null,null,null);
                tv.setCompoundDrawablePadding(drawablePadding);
            }
        }
        addView(tv, p);
        AutoUtils.autoTextSize(tv);
    }

    private void createMoreImage()
    {
        if(itemRightIcon != 0)
        {
            ImageView iv = new ImageView(mContext);
            iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
            iv.setImageResource(itemRightIcon);
            LayoutParams p = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            p.addRule(RelativeLayout.CENTER_VERTICAL);
            p.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            addView(iv, p);
        }
    }
}
